import { AIPropertyDescriptor, IField } from "../AIProperty";
import { Field } from "./Field";

export class RangeField extends Field implements IField {
    create(o: AIPropertyDescriptor, data: any) {
        o.min = o.range[0];
        o.max = o.range[1];
        let p = this._create(o, 'Range', data, 0);
        let numInput = this._panel.getChild("numInput") as IEditor.NumericInput;
        numInput.on('submit', () => {
            this.data[this.o.name] = numInput.value;
        });
        let r = this._panel.getChild("value") as gui.Slider;
        numInput.min = r.min;
        numInput.max = r.max;
        if (null != o.step)
            numInput.step = o.step;
        this._dataWatch();

        IEditor.DataWatcher.addListener(data, this._dataWatch, this);

        return p;
    }
    protected _changeHandle(e: gui.Event) {
        let r = this._panel.getChild("value") as gui.Slider;
        this.data[this.o.name] = r.value;
        this._checkChange();
    }

    private _dataWatch() {
        let val = this.data[this.o.name];
        let r = this._panel.getChild("value") as gui.Slider;
        r.value = val;
        let numInput = this._panel.getChild("numInput") as IEditor.NumericInput;
        numInput.value = val;
    }

    protected _setValue(val: any): void {
        let r = this._panel.getChild("value") as gui.Slider;
        r.value = val;

        let numInput = this._panel.getChild("numInput") as IEditor.NumericInput;
        numInput.value = val;
    }
}